import sounddevice as sd
import numpy as np

def play_pcm_file(pcm_file, fs=16000):
    """
    播放 PCM 文件
    :param pcm_file: PCM 文件路径
    :param fs: 采样率，默认为 16000 Hz
    """
    try:
        # 读取 PCM 文件
        with open(pcm_file, 'rb') as f:
            audio_data = np.fromfile(f, dtype=np.int16)
        
        # 播放音频数据
        # print("开始播放...")
        sd.play(audio_data, samplerate=fs)
        sd.wait()  # 等待播放完成
        print("播放结束")
    except Exception as e:
        print(f"播放失败: {e}")

# 示例使用
if __name__ == "__main__":
    pcm_file = "../pcmfiles/outputPcmData.pcm"  # 替换为你的 PCM 文件路径 # "E:\\ai-chat-client\\ai-chat-client\\pcmfiles\\outputPcmData.pcm"
    play_pcm_file(pcm_file)